﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entity;
using BusinessObject;
using System.IO;

namespace GreenLife.Admin
{
    public partial class ChinhSuaDanhMucCon : System.Web.UI.Page
    {
        DataSet ds;
        SubCategory sc;
        SubCategory sc1;
        SubCategoriesBO scBO = new SubCategoriesBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                selectSubCateByID();
            }
        }

        public void selectSubCateByID()
        {
            sc = new SubCategory();
            int id = Int32.Parse(Request.QueryString["id"]);
            sc = scBO.selectSubCategoryByID(id);
            txtSubCateId.Text = sc.SubCategory_ID.ToString();
            txtCateId.Text = sc.Category_ID.ToString();
            txtSubCateName.Text = sc.SubCategory_Name.ToString();
            imgSubCate.ImageUrl = sc.SubCategory_Image.ToString();
            txtDescription.Text = sc.SubCategory_Description.ToString();
            txtSubCateArea.Text = sc.SubCategory_Area.ToString();
            txtSubCateHuong.Text = sc.SubCategory_Huong.ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Int32.Parse(Request.QueryString["id"]);
            sc1 = scBO.selectSubCategoryByID(id);

            sc = new SubCategory();
            sc.SubCategory_ID = Int32.Parse(txtSubCateId.Text);
            sc.Category_ID = Int32.Parse(txtCateId.Text);
            sc.SubCategory_Name = txtSubCateName.Text;
            if (Path.GetFileName(FileUploadControl.FileName).Equals("") == true)
            {
                sc.SubCategory_Image = sc1.SubCategory_Image.ToString();
            }
            else
            {
                sc.SubCategory_Image = "~/imageUpload/" + Path.GetFileName(FileUploadControl.FileName);
            }
            sc.SubCategory_Description = txtDescription.Text;
            sc.SubCategory_Area = float.Parse(txtSubCateArea.Text);
            sc.SubCategory_Huong = txtSubCateHuong.Text;
            if (scBO.updateSubCategory(sc))
            {
                Response.Redirect("DanhSachDanhMucCon.aspx");
            }
        }
    }
}